package com.sinaapp.gavinzhang.jianzhioffer;

/**
 * Created by gavin on 15-9-4.
 */
public class MoniPower {
    public static void main(String[] args)
    {
        System.out.println(Power(2.0,2));
        System.out.println(Pow(2.0,3));
    }

    public static double Pow(double base,int exponent)
    {
        if (exponent == 0)
        {
            return 1;
        }
        if (exponent == 1)
        {
            return base;
        }

        double result = Pow(base,exponent>>1);
        result *=result;
        if ((exponent&1) == 1){
            result *=base;
        }
        return result;
    }

    public static double Power(double base,int exponent)
    {
        int p = Math.abs(exponent);
        double r = 1.0;
        while(p != 0){
            if((p&1) != 0) r *= base;
            base *= base;
            p >>= 1;
        }
        return exponent<0?1/r:r;
    }
}
